﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FurnitureSystem
{
    public partial class frmEdit : Form
    {
        public frmEdit()
        {
            InitializeComponent();
        }

        private void txtUpdate_Click(object sender, EventArgs e)
        {
            SqlConnection conn = connect.Getconect();
            conn.Open();
            string name= txtName.Text;
            string Addr=txtAddress.Text;
            string Contact=txtContact.Text;
            string Op=txtOldPass.Text;
            string np=txtNewPass.Text;
            string nrp=txtNewRePass.Text;
            if (np != "")
            {
                if (np == nrp)
                {
                    string sql = "update EmployeeDetail set staffname='" + name + "',Addr='" + Addr + "',Contact_no='" + Contact + "',password='" + np + "'where staffid='" + frmManagerUser.staffid + "'";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    SqlDataReader sdr = cmd.ExecuteReader();
                    MessageBox.Show("Cập nhật thành công");
                    frmManagerUser fmu = new frmManagerUser();
                    fmu.Show();
                    this.Dispose();
                }
                else
                {
                    MessageBox.Show("Mật khẩu phải trùng nhau");
                }
            }
            else {
                string sql = "update EmployeeDetail set staffname='" + name + "',addr='" + Addr + "',contact_no='" + Contact + "' where staffid='"+frmManagerUser.staffid+"'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                MessageBox.Show("Cập nhật thành công");
                frmManagerUser fmu = new frmManagerUser();
                fmu.Show();
                this.Dispose();

            } 

        }

        private void frmEdit_Load(object sender, EventArgs e)
        {
            SqlConnection conn = connect.Getconect();
            try
            {
                conn.Open();
                string sql = "select * from EmployeeDetail where staffid='" + frmManagerUser.staffid + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    txtName.Text = sdr.GetString(1);
                    txtAddress.Text = sdr.GetString(2);
                    txtContact.Text = sdr.GetInt32(3).ToString();
                    txtOldPass.Text = sdr.GetString(4);
                }
            }
            catch (SqlException ex){
                MessageBox.Show(ex.Message);
            }
            conn.Close();
        }

        private void txtCancel_Click(object sender, EventArgs e)
        {
            frmManagerUser fmu = new frmManagerUser();
            fmu.Show();
            this.Dispose();
        }
    }
}
